package com.pet.server.ext.action.user;

import java.util.Date;

import net.sf.json.JSONObject;

import com.pet.server.ext.ActionBase;
import com.pet.server.ext.ApiError;
import com.pet.server.ext.BeanToJson;
import com.pet.server.ext.Result;
import com.pet.server.ext.data.PetInfo;
import com.pet.server.ext.service.UserService;
import com.pet.server.ext.util.DateUtil;
import com.pet.server.ext.util.JsonUtil;
import com.pet.server.ext.util.NumberUtil;
import com.pet.server.ext.util.Parameters;
import com.pet.server.ext.util.StringUtil;
import com.pet.server.ext.util.cache.ConstantUtil;

public class Petadd extends ActionBase{
	
	public String pet_cate,pet_name,breed,ico_path,sex;
	public Date birthday;
	
	
	public Petadd(){
		 needCheckAuth =true;
	}
	
	@Override
	public void initParameters(Parameters params) throws ApiError {	
		pet_cate = params.getFieldValue("pet_cate");
		pet_name = params.getFieldValue("pet_name");
		breed = params.getFieldValue("breed");
		ico_path = params.getFileUrl("ico_path");
		birthday = DateUtil.getDate(NumberUtil.getLong(params.getFieldValue("birthday"), 0l));
		sex = params.getFieldValue("sex");//male female
		if(StringUtil.isEmpty(pet_cate,pet_name,breed,birthday,sex)){
			throw ApiError.Type.INVALID_PARAM.toException();
		}
	}
	@Override
	public Result invokeService() throws ApiError {
		UserService userService = new UserService();
		PetInfo pet = new PetInfo();
		pet.birthday=birthday;
		pet.pet_cate=pet_cate;
		pet.breed=breed;
		pet.ico_path=ico_path;
		pet.pet_name=pet_name;
		pet.sex=sex;
		pet.create_time=new Date();
		pet.user_id=user.user_id;
		userService.savePetInfo(pet);
		JSONObject data = new JSONObject();
		data.accumulate("massage", "添加成功");
		Result result = new Result();
		data.put("birthday", JsonUtil.getTime(pet.birthday));
        data.put("pet_cate", pet.pet_cate);
        data.put("breed", pet.breed);
        data.put("breed_name", ConstantUtil.getInstance().getName(pet.breed));
        data.put("ico_path", pet.ico_path);
        data.put("sex", pet.sex);
        data.put("create_time", JsonUtil.getTime(pet.create_time));
        data.put("user_id", pet.user_id);
        data.put("pet_id", pet.pet_id);
		result.put("data", data);
	    return result;
	}
}
